package com.zh.web;

import com.zh.po.Order;
import com.zh.po.Product;
import com.zh.po.UserProduct;
import com.zh.service.OrderService;
import com.zh.service.ProductService;
import com.zh.service.UserProductService;
import com.zh.web.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author ふわふわ
 * @version 1.0
 * @description:
 * @date 2021/4/15 20:18
 */
@RestController
@CrossOrigin
@RequestMapping("/userProduct")
public class UserProductController {
    @Autowired
    private UserProductService userProductService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    /**
     * @description: 加入购物车
     * @param: userProduct 购物车
     * @return: com.zh.web.vo.ResponseVO
     */
    @PostMapping
    public ResponseVO addShoppingCart(@RequestBody UserProduct userProduct){
        if (userProduct==null){
            return new ResponseVO<>(null,"加入购物车失败",4000);
        }
        Integer productId = userProduct.getProductId();
        UserProduct userProduct1 = userProductService.findByProductId(userProduct.getUserId(),productId);
        if (userProduct1 != null ){
            if (userProduct1.getUserId().equals(userProduct.getUserId())){
                return new ResponseVO<>(null,"商品已在购物车中",4000);
            }else {
                userProductService.addShoppingCart(userProduct);
                return new ResponseVO<>(null,"加入购物车成功",2000);
            }
        }else {
            userProductService.addShoppingCart(userProduct);
            return new ResponseVO<>(null,"加入购物车成功",2000);
        }
    }

    /**
     * @description:  删除购物车
     * @param: ids 购物车编号
     * @param: userId 用户编号
     * @return: com.zh.web.vo.ResponseVO
     */
    @DeleteMapping("/{userId}")
    public ResponseVO deleteShoppingCart(@RequestBody Integer[] ids,@PathVariable Integer userId){
        int row =0;
        for (Integer id : ids) {
            row = userProductService.deleteShoppingCart(userId,id);
        }
        if (row >0){
            return new ResponseVO<>(row,"删除购物车成功",2000);
        }
        return new ResponseVO<>(null,"删除购物车失败",4000);
    }

    /**
     * @description: 修改购物车
     * @return: com.zh.web.vo.ResponseVO
     * @author ふわふわ
     * @date: 2021/5/12 10:17
     */
    @GetMapping("/updateCart/{userId}/{productId}/{productBuyNum}")
    public ResponseVO updateCart(@PathVariable Integer userId,@PathVariable Integer productId,@PathVariable Integer productBuyNum){
        UserProduct byProductId = userProductService.findByProductId(userId, productId);
        Product productById = productService.getProductById(productId);
        Integer beforeNum=byProductId.getProductBuyNum();
        if (productBuyNum==null || productBuyNum==0){
            userProductService.deleteShoppingCart(productId,userId);
        }
        productById.setProductNum(productById.getProductNum()+beforeNum-productBuyNum);//修改库存数量
        productService.updateProduct(productById);
        byProductId.setProductBuyNum(productBuyNum);
        Double productPrice = productById.getProductPrice();
        byProductId.setSubtotal(productBuyNum*productPrice);
        userProductService.updateCart(byProductId);
        return new ResponseVO<>(null,"修改购物车成功",4000);
    }

    @GetMapping("/deleteOneCart/{userId}/{productId}")
    public ResponseVO deleteOneCart( @PathVariable Integer userId, @PathVariable Integer productId){
        UserProduct byProductId = userProductService.findByProductId(userId, productId);
        Product productById = productService.getProductById(productId);
        productById.setProductNum(productById.getProductNum()+byProductId.getProductBuyNum());
        productService.updateProduct(productById);
        int row = userProductService.deleteShoppingCart(userId,productId);
        if (row >0){
            return new ResponseVO<>(row,"删除购物车成功",2000);
        }
        return new ResponseVO<>(null,"删除购物车失败",4000);
    }
    /**
     * @description: 提交订单
     */
    @GetMapping("/commitCart/{userId}/{orderAddress}")
    public ResponseVO commitCart( @PathVariable Integer userId,@PathVariable String orderAddress){
        if (orderAddress==null){
            return new ResponseVO<>(null,"请输入收货地址",4000);
        }
        List<UserProduct> allCartByUserId = userProductService.getAllCartByUserId(userId);
        for (UserProduct userProduct : allCartByUserId) {
            Order order = new Order(userProduct.getUserId(), userProduct.getProductId(), userProduct.getProductBuyNum(), userProduct.getSubtotal(), orderAddress, new Date(), 4);
            userProductService.deleteShoppingCart(userId,userProduct.getProductId());
            orderService.addOrder(order);
        }
        return new ResponseVO<>(null,"添加订单成功",2000);
    }
}
